/**
 * Created by a1 on 2017/10/23.
 */
"use strict";
var fs = require('fs');
var formidable = require('formidable');
var common = require('../models/common.js');
var BookModel=require('../models/Book.js');
var BookshelfModel=require('../models/Bookshelf.js');

exports.tickeKey=function(req,res){
    if(req.session.username){
        if(req.params.key=="month"){
            res.render('ticket',{
                page:'month',
                title:'月票'
            });
        }else if(req.params.key=="recomm"){
            res.render('ticket',{
                page:'recomm',
                title:'推荐票'
            });
        }else{
            res.render('error');
        }
    }else{
        res.render('login',{
            page:'login'
        });
    }
};
exports.bookshelfKey=function(req,res){
    if(req.params.key=="my"){
        if(req.session.username){
            res.render('my',{
                page:'my',
                session:true
            });
        }else{
            res.render('my',{
                page:'my',
                session:false
            });
        }
    }else if(req.params.key=="history"){
        if(req.session.username){
            res.render('my',{
                page:'history',
                session:true
            });
        }else{
            res.render('my',{
                page:'history',
                session:false
            });
        }
    }else{
        res.render('error',{page:'error'});
    }
};
exports.bookcase=function(req,res){
    if(req.session.uid){
        BookshelfModel.findSortOne({uid:req.session.uid},{bsid:1},function(err,docs){
            //console.log(docs);
            if(docs==null){
                res.json({
                    code:0,
                    msg:'查询数据出错'
                });
                return;
            }else{
                res.json({
                    code:1,
                    bookshelf:docs['bookshelf'],
                    msg:'成功'
                });
            }
        });
    }else{
        res.send();
    }
};
exports.removeBookcase=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let bid=fields.bid;
        let sjid=fields.sjid;
        let booksId=fields.booksId;
        let sjidnum=parseInt(sjid-1);
        var tiaojian="bookshelf."+sjidnum+".books";
        var pull= `{"${tiaojian}":{"bid":${bid}}}`;
        pull=JSON.parse(pull);
        BookshelfModel.updateFn({"uid":req.session.uid},{$pull:pull},function(err,docs){
            if(err){
                res.json({"msg":'失败',"code":0});
                return;
            }
            //收藏数减1
            BookModel.updateFn({bid:bid},{$inc:{recomendCnt:-1}},function(err,result){
                if(err){
                    res.json({"msg":'失败',"code":0});
                    return;
                }
                res.json({"msg":'成功',"code":1});
            });
        });
    });
};
exports.addBookShelf=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files) {
        let bid = fields.bid;
        let sjid = fields.sjid;
        let booksId = fields.booksId;
        let val = fields.val;
        let key = fields.key;
        let sjidnum = parseInt(sjid) - 1;
        BookshelfModel.findSortOne({uid:req.session.uid},{'bsid':1},function(err,docs){
            if(key=="true"){
                let obj={
                    sjid:(docs['bookshelf'].length+1),
                    sjname:val,
                    books:[docs['bookshelf'][sjidnum]['books'][booksId]]
                };
                BookshelfModel.updateFn({uid:req.session.uid},{$push:{"bookshelf":obj}},function(err,docs1){
                    res.json({"msg":'成功',"code":1});
                })
            }else{
                let booksarr=docs['bookshelf'][sjidnum]['books'][booksId];
                let objtxt='{"bookshelf.'+sjid+'.books":'+JSON.stringify(booksarr)+'}';
                let pushobj = JSON.parse(objtxt);
                BookshelfModel.updateFn({uid:req.session.uid},{$push:pushobj},function(err,docs1){
                    res.json({"msg":'成功',"code":1});
                })
            }
        });
    });
};
exports.moveBookShelf=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files) {
        let bid = parseInt(fields.bid);
        let sjid = parseInt(fields.sjid);
        let sjid1 = parseInt(fields.sjid1);
        let booksId = parseInt(fields.booksId);
        let sjidnum = sjid - 1;
        let sjidnum1 = sjid1 - 1;
        console.log(bid);
        BookshelfModel.findSortOne({uid:req.session.uid},{'bsid':1},function(err,docs){
            let booksarr=docs['bookshelf'][sjidnum]['books'][booksId];
            let objtxt='{"bookshelf.'+sjidnum1+'.books":'+JSON.stringify(booksarr)+'}';
            let pushobj = JSON.parse(objtxt);
            BookshelfModel.updateFn({uid:req.session.uid},{$push:pushobj},function(err,docs1){
                res.json({"msg":'成功',"code":1});
            });
        });
    });
};